<?php
    header("Content-type:text/html;charset=utf-8");
    require('./conn/mysql.php');

    $type = $_POST['type'];

    try{
        // let obj = {
        //     cart_id: item.cart_id,
        //     user_id: item.user_id,
        //     goods_id: item.goods_id,
        //     cart_num: item.cart_num,
        //     total: item.cart_num * item.goods_new_price
        // };

        if('add' == $type){
            $list = $_POST['data'];
            $user_id = $_POST['user_id'];
            $total = $_POST['total'];
            $sjr = $_POST['sjr'];
            $sjdz = $_POST['sjdz'];
            $tel = $_POST['tel'];

            $data = array(
                'user_id'=> $user_id,
                'order_no'=> 'I'.time(),
                'add_time'=> date('Y-m-d H:i:s', time()),
                'total_money'=> $total,
                'sjr'=> $sjr,
                'sjdz'=> $sjdz,
                'tel'=> $tel,
            );
            $res = mysql::exec('orders', $data, 'insert');
            if(!$res) die(write(0, '提交失败!'));
            $order_id = mysql::getLastId();

            foreach ($list as $key => $value) {
                if(is_array($value)){
                    $cart_id =  $value['cart_id'];
                    $sql = "delete from `cart` where `cart_id` = $cart_id and `user_id` = $user_id";
                    mysql::query($sql);


                    unset($value['cart_id']);
                    $value['order_id'] = $order_id;
                    mysql::exec('orders_detail', $value, 'insert');
                }
            }
            echo write(1, '提交成功!');
        }
    }catch(\Throwable $t){
        echo write(0, ''.$t);
    }finally{
         mysql::close();
    }
    function write($code, $msg, $data=[]){
        $rd = array(
            'code' => $code,
            'msg' => $msg,
            'data' => $data,
        );
        return json_encode($rd, JSON_UNESCAPED_UNICODE);
    }
?>